/********************************
 *版权所有 CopyRight(c) 快程乐码信息有限公司所有，未经授权，不得复制、转发
 */
package com.yjx.test.dozer.config;

import com.yjx.test.dozer.entity.Account;
import com.yjx.test.dozer.entity.AccountApiDest;
import com.yjx.test.dozer.entity.Name;
import org.dozer.DozerBeanMapperBuilder;
import org.dozer.Mapper;
import org.dozer.classmap.RelationshipType;
import org.dozer.loader.api.BeanMappingBuilder;
import org.dozer.loader.api.FieldsMappingOptions;
import org.dozer.loader.api.TypeMappingBuilder;
import org.dozer.loader.api.TypeMappingOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

import static org.dozer.loader.api.FieldsMappingOptions.*;
import static org.dozer.loader.api.TypeMappingOptions.*;

/************
 *@Author yejx
 *@version v1.0
 *@create 2020/6/29 13:32
 *@Description TODO
 *
 */
@Configuration
public class DozerMapperConfig {


    @Bean
    public Mapper mapper() {
        Mapper mapper = DozerBeanMapperBuilder.create()
                //指定 dozer mapping 的配置文件(放到 resources 类路径下即可)，可添加多个 xml 文件，用逗号隔开
                //   .withMappingFiles("dozerBeanMapping.xml")
                .withMappingBuilder(beanMappingBuilder())
                .build();
        return mapper;
    }

    private BeanMappingBuilder beanMappingBuilder() {
        BeanMappingBuilder builder = new BeanMappingBuilder() {
            protected void configure() {
                //利用代码做映射
                //    mapping(Account.class, AccountApiDest.class).fields("aid","id");
                mapping(Name.class, HashMap.class, wildcard(false)).exclude("firstName").fields("fullName", "my", hintA(java.lang.String.class));
            }
        };
        return builder;
    }
}
 /*     //一个映射元素都有多个映射元素，伴随着类级别的映射声明和域级别的映射
                // ，wildcard属性在默认的情况是true,这意味着那将自动地尝试映射两个类中的每一个属性，
                // 当wildcard属性被设置为false的时候，仅仅映射显示定义的映射域。
                //exclude("field") 这个属性不做映射
                //copyByReference按对象复制
                //mapId 基于上下文的映射
  mapping(Bean.class, Bean.class, TypeMappingOptions.oneWay(), mapId("A"), mapNull(true),wildcard(true))
          .exclude("excluded")
          .fields("src", "dest", copyByReference(), collectionStrategy(true, RelationshipType.NON_CUMULATIVE),
          hintA(String.class),
          hintB(Integer.class),
          FieldsMappingOptions.oneWay(),
          useMapId("A"),
          customConverterId("id")).fields("src", "dest", customConverter("org.dozer.CustomConverter"));
*/